<template>
    <div class="suspension">
        <div class="content" :style="contentStyle">
            <div class="button">按钮1</div>
            <div class="button">按钮2</div>
            <div class="button">按钮3</div>
            <div class="button">按钮4</div>
            <div class="button">按钮5</div>
        </div>
        <div class="icon" @click="toggle">
            <span v-if="open">&langle;</span>
            <span v-else>&rang;</span>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        // 加载悬浮栏的初始状态
        const open = localStorage.getItem('suspensionOpen') === null ? true : localStorage.getItem('suspensionOpen') === 'true';
        return {
            open,
            contentStyle: { // 用于控制内容的显示与隐藏
                width: open ? '' : '0',
            }
        }
    },
    methods: {
        toggle() {
            if (this.open) {
                // 现打开状态，下面时关闭操作
                this.contentStyle.width = '0';
                // 动画结束后，将内容隐藏
                setTimeout(() => {
                    this.open = false;
                    this.keep();
                }, 400);
            } else {
                // 现关闭状态，下面时打开操作
                this.contentStyle.width = '';
                // 动画完成后，再设置open为true
                setTimeout(() => {
                    this.open = true;
                    this.keep();
                }, 400);
            }
        },
        // 保持状态
        keep() {
            localStorage.setItem('suspensionOpen', this.open);
        }
    }
}
</script>

<style scoped>
.suspension {
    position: absolute;
    bottom: .2em;
    left: .2em;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    border-radius: .3em;
    overflow: hidden;
    height: 3em;
    line-height: 3em;
    width: auto;
}

.suspension .content {
    display: block;
    white-space: nowrap;
    background-color: #ffffff;
    color: white;
    max-width: calc(100vw - 2em);
    width: 100%;
    height: 100%;
    overflow-x: auto;
    transition: all .5s;
}

.button {
    width: 4em;
    height: 1.5em;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-wrap: nowrap;
    background-color: #ffffff;
    color: #000000;
    border: #d72618 solid 1px;
    border-radius: 1em;
    margin: .1em;
    white-space: nowrap;
    overflow: hidden;
}

.suspension .icon {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #ffffff;
    color: #878787;
    font-size: 1.5em;
    cursor: pointer;
    user-select: none;
}

.suspension .icon span {
    position: relative;
    top: -.1em;
}
</style>